<template>
  <div>
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>数据采集</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片导航 -->
      <div style="height:300px;width:400px;overflow-y:auto">
          <h3>数据表列表</h3>
          <el-card class="box-card">
            成绩分析项表
            <div v-for="o in 50" :key="o" class="text item">
              {{'列表内容 ' + o }}
            </div>
          </el-card>
      </div>
      <div class="show"></div>
      <!-- 右侧数据库中的数据表格 -->
      <div class="content">
        <!-- 用户列表 -->
      <el-table :data="userData.userList" stripe style="width: 100%" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"></el-table-column>
        <el-table-column label="状态">
          <template v-slot="scope">
            <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ff4949" @change="userStatuChanged(scope.row)"> </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById(scope.row.id)"></el-button>
            <!-- 分配角色按钮 -->
            <el-tooltip effect="dark" content="分配角色" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRoles(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        @size-change="handleSizeChange"
        :page-size="queryInfo.pagesize"
        :page-sizes="[5]"
        :total="userData.total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
        <!-- 插入分页器，调整每页显示的条数 -->
      <div>
      </div>
      </div>
      <!-- 数据采集实现按钮 -->
      <div class="get_data">
        <el-button class="gets">导入</el-button>
        <el-button class="gets">调用摄像头</el-button>
        <el-button class="gets">保存</el-button>
      </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        // 搜索值
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每次显示多少条数据
        pagesize: 5
      },
      userData: {
        userList: [],
        total: 0
      }
    }
  },
  methods: {
    async getUserList() {
      const { data: res } = await this.$http.get('users', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取用户列表失败!')
      }
      this.$message.success('获取用户列表成功!')
      this.userData.userList = res.data.users
      this.userData.total = res.data.total
      // console.log(res)
    },
    // 监听 pagesize 改变事件 每页显示的个数
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 监听 页码值 改变的事件 当前页面值
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },
    // 监听Switch状态的改变
    async userStatuChanged(userInfo) {
      // console.log(userInfo)
      const { data: res } = await this.$http.put(`users/${userInfo.id}/state/${userInfo.mg_state}`)
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state
        return this.$message.error('更新用户状态失败!')
      }
      return this.$message.success('更新用户状态成功!')
    }
  }
}
</script>

<style>
 .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 10%;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 80%;
  }
  .show{
    position: absolute;
    width: 20%;
    height: 180px;
    background-color: rgb(235, 78, 78);
    background-image:url('../../../img/lcx.jpg');
    background-size:cover;
    bottom: 20px;
    left: 17%;
  }
  .content{
    position:absolute;
    width: 50%;
    height: 70%;
    top:18%;
    left: 45%;
  }
  .get_data{
    position:absolute;
    bottom: 0;
    left: 65%;
  }
  .pages{
    position: absolute;
    right: 230px;
    bottom: 50px;
  }
</style>
